package com.imodia.checkoo.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.html.BaseUrl;
import com.imodia.checkoo.bean.CmsManagerMenuBean;
import com.imodia.checkoo.bean.CmsPageLayoutBean;
import com.imodia.checkoo.service.impl.CmsManagerMenuServiceImpl;


@Controller
@RequestMapping("/hfmanager/popedom/list.do")
public class CmsPopedomController {
	
	private final String url_list = "/hfmanager/manager_menu/popedom_list";

	@Autowired
	private CmsManagerMenuServiceImpl cmsManagerMenuService;
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsManagerMenuBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		SqlParam sql = new SqlParam();
		sql.andEqualTo("parent_id", request.getParameter("parentId"));
		sql.andEqualTo("rs_id", "9");
		sql.andOrderListValue("priority", "");
		
		PageResult<CmsManagerMenuBean> pageResult =cmsManagerMenuService.page(sql,page);
		model.put("page", pageResult);
		return new ModelAndView(url_list, model);
	}
	
	
	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsManagerMenuBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsManagerMenuService.update(obj);
		}else{
			x = cmsManagerMenuService.insert(obj);
		}
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("message", String.valueOf(x));
		return new ModelAndView(BaseUrl.message, model);
	}
	
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsManagerMenuService.delete(id);
		int parentId = StringUtil.requestInt(request, "parentId");
		return new ModelAndView(new RedirectView(request.getContextPath()+"/hfmanager/popedom/list.do?action=list&parentId="+parentId), null);
	}

}
